using System;
using System.ComponentModel;
using RollManager.Library.Utils;

namespace RollManager.Library.ActionSteps
{
    [Serializable]
    public class ConnectNetShareStep : ActionStep
    {
        private string m_share = StepConstants.PATH;
        private string m_user = "username";
        private string m_pass = "password";

        [DisplayName("Password")]
        [Description("The password needed to access the given share.")]
        public string Pass
        {
            get
            {
                return m_pass;
            }
            set
            {
                m_pass = value;
            }
        }

        [DisplayName( "Share Path" )]
        [Description( "The UNC path to the share to be accessed." )]
        public string Share
        {
            get
            {
                return m_share;
            }   
            set
            {
                if(!string.IsNullOrEmpty( value))
                {
                    m_share = value.Trim();
                }
                else
                {
                    throw new ArgumentException( "Share Path must be specified." );
                }
            }
        }

        [DisplayName( "Username" )]
        [Description( "The username needed to access the given share." )]
        public string User
        {
            get
            {
                return m_user;
            }
            set
            {
                if( !string.IsNullOrEmpty( value ) )
                {
                    m_user = value.Trim();
                }
                else
                {
                    throw new ArgumentException( "Username must be specified." );
                }
            }
        }

        public override string Name
        {
            get { return "Connect Network Share"; }
        }

        protected override void Run()
        {
            Common.RunCommand( @"C:\WINDOWS\system32\net", 
                string.Format( @"use {0} {1} /USER:{2}", Share, Pass, User ), Progress );
        }

        protected override void Simulate()
        {
            if( string.IsNullOrEmpty( m_share ) )
            {
                throw new Exception( string.Format( "Share Path was empty." ) );
            }

            if( string.IsNullOrEmpty( m_pass ) )
            {
                throw new Exception( string.Format( "Password was empty." ) );
            }

            if( string.IsNullOrEmpty( m_user ) )
            {
                throw new Exception( string.Format( "Username was empty." ) );
            }

            Progress.AddFormat( "Simulated connection to {0} with username {1} and password {2}",
                m_share, m_user, m_pass );
        }
    }
}
